<?php
/**
 * The form for inputting a new order into the database.
 * @author Andy Rich <andy.rich@gmail.com>
 * @version 1.0
 * @since 2013-11-02
 */

require_once 'login_info.php';
require_once 'database.php';

loginInfo::update_login ();
$db = new database ();

$customers = $db->query("SELECT id, name FROM Customers");

?><html>
	<head>
		<title>Opol - Add new order</title>
		<style type="text/css">
			label{
				display: inline-block;
				float: left;
				clear: left;
				width: 80px;
				text-align: right;
				padding-right: 10px;
			}
			input, select, textarea {
				display: inline-block;
				float: left;
				margin-bottom: 10px;
			}
		</style>
	</head>
	<body>
		<link rel="stylesheet" type="text/css" href="opol.css">
		<?php require 'menu.php'; ?>
		<br>
<?php
if ($_POST) {
	$new_order = $db->prepare("INSERT INTO Orders VALUES (NULL, ?, ?, b'0', ?, ?, 0, ?, ?)");

	$cust = $_POST["customer_id"];
	$addr = $_POST["shipping_addr"];
	$pri = (int)$_POST["priority"];
	$instr = $_POST["special_instructions"];
	$quantity = (int)$_POST['quantity'];
	$due = $_POST['due-year'] . "-" . $_POST['due-month'] . "-" . $_POST['due-day'];
	$new_order->bind_param("ssiiss", $cust, $addr, $pri, $quantity, $instr, $due);

	$new_order->execute();
	$order_number = $db->insert_id();

	if ($order_number != 0) {
		print "<h2>Order #" . $order_number . " Created!</h2>";
		print "<a href='new_phase.php?order_id=$order_number'>Click here to add phases</a>";

	} else {
		echo "You do not have sufficient permissions to perform this action.<br>\n";
	}
}
?>
		<h1>Add a new order</h1>
		<form name="text" action="new_order.php" method="post">
			<label>Customer: </label> 
				<select name="customer_id">
<?php
while ($row = $customers->fetch_assoc()){
	$id = $row['id']; $name = $row['name'];
	print "\t\t\t\t\t<option value='$id'>$name</option>\n";
}

?>				</select> <br>
			<label>Deadline: </label>
					<select name="due-month">
						<option value="1">Jan</option>
						<option value="2">Feb</option>
						<option value="3">Mar</option>
						<option value="4">Apr</option>
						<option value="5">May</option>
						<option value="6">Jun</option>
						<option value="7">Jul</option>
						<option value="8">Aug</option>
						<option value="9">Sep</option>
						<option value="10">Oct</option>
						<option value="11">Nov</option>
						<option value="12">Dec</option>
					</select>
					<select name="due-day">
<?
foreach (range(1,31) as $i) {
	print "\t\t\t\t\t<option value='$i'>$i</option>\n";
}

?>					</select>
					<select name="due-year">
						<option value="2013">2013</option>
						<option value="2014">2014</option>
						<option value="2015">2015</option>
					</select>
			<label>Priority: </label>
				<select name="priority">
					<option value="1">Highest</option>
					<option value="2">Higher</option>
					<option value="3" selected>Normal</option>
					<option value="4">Lower</option>
					<option value="5">Lowest</option>
				</select><br>
				<label>Quantity: </label>
				<input type='text' name='quantity'><br>
			<label>Shipping Address: </label>
				<textarea name="shipping_addr" rows="4" cols="25"></textarea> <br>
			<label>Comments: </label>
				<textarea name="special_instructions" rows="4" cols="25"></textarea><br>
			<label>&nbsp;</label>
				<input type="submit" value="Save"> <br>
		</form>
	</body>
</html>
  
